#include <cstdio>
#include <fstream>
#include <cstring>
#include <vector>

#include "types.h"
#include "functions.h"

using namespace std;

bool statClickFile(const char *clickfile)
{
	printf(">>> statistics click file '%s' :\n", clickfile);
	
	ifstream fin;
	ofstream fout;
	
	fin.open(clickfile, ios::in);
	if (!fin.is_open())	// input file open failure
	{
		OpenFileFailed(clickfile);
		return false;
	}
	
	
	char *statfile = NULL;
	formFilename(&statfile, clickfile, STAT);
	fout.open(statfile, ios::out);
	if (!fout.is_open())	// output file open failure
	{
		OpenFileFailed(statfile);
		fin.close();
		return false;
	}
	
	int maxQuerylen = 0;
	int maxURLlen = 0;
	int totalclicks = 0;
	int totalqueries = 0;
	int mostclicks = 0;
	int clickcount = 1;
	int lineno = 1;
	
	char curline[CLICKLINESIZE];
	char tmpID[IDLEN + 1];
	uint64 curID = 0;
	uint64 preID = 0;
	fin.getline(curline, CLICKLINESIZE, '\n'); 
	
	while (!fin.eof())
	{
		fin.getline(curline, CLICKLINESIZE, '\n');
		if (fin.gcount() == 0)	// the end of file is reached 
		{
			printf(">>> line %d : file '%s' end\n", lineno, clickfile);
			break;
		}
		lineno++;
		totalclicks++;	// a "real" click
		
		// parse information needed
		strncpy(tmpID, curline, IDLEN);
		tmpID[IDLEN] = 0;
		hexstr2uint64(tmpID, &curID);
		char *ptq = strchr(curline, '\t');	// pointer to the '\t' before query
		char *ptt = strchr(ptq + 1, '\t');	// pointer to the '\t' before time
		char *ptu = strchr(ptt + 1, '\t');	// pointer to the '\t' before URL
		char *ptp = strchr(ptu + 1, '\t');	// pointer to the '\t' before position
		int Querylen = ptt - ptq - 1;
		int URLlen = ptp - ptu - 1;
		if (Querylen > maxQuerylen)
			maxQuerylen = Querylen;
		if (URLlen > maxURLlen)
			maxURLlen = URLlen;
		
		if (preID == curID)
			clickcount++;
		else	// a new query
		{
			if (clickcount > mostclicks)
				mostclicks = clickcount;
			clickcount = 1;
			totalqueries++;
			preID = curID;
		}
		
		if (lineno % PROGRESS == 0)
			printf("line %d\n", lineno);
	}
	
	fout << "total lines = " << lineno << endl
		<< "total clicks = " << totalclicks << endl
		<< "total queries = " << totalqueries <<endl
		<< "maximum of query length = " << maxQuerylen <<endl
		<< "maximum of URL length = " << maxURLlen <<endl
		<< "most clicks of one query = " << mostclicks <<endl;
	
	fin.close();
	fout.flush();	// write the unwritten characters in the buffer to file(assume no error here)
	fout.close();
	printf(">>> stat OK: file '%s' ==> file '%s'\n", clickfile, statfile);
	delete [] statfile;
	return true;
}

void statClicks()
{
	char *clickfiles[]={
		"srfp20060501-20060531.clicks.adult.txt",
		"srfp20060501-20060531.clicks.txt",
	};
	
	char c, b;
	printf("statistic file:\n");
	printf("1: %s\n", clickfiles[0]);
	printf("2: %s\n", clickfiles[1]);
	printf("3: both 1 and 2\n");
	printf("0: exit\n");
	printf("choice: ");
	c = GetChar();
	switch (c)
	{
		case '3':
		case '1':	if (!statClickFile(clickfiles[0]))
						printf(">>> ERROR: file '%s' failed\n", clickfiles[0]);
					if (c == '1') break;
					else
					{
						printf("Continue...(Y/n)? ");
						b = GetChar();
						if (b == 'n' || b == 'N')	break;
					}
		case '2':	if (!statClickFile(clickfiles[1]))
						printf(">>> ERROR: file '%s' failed\n", clickfiles[1]);
		case '0':	break;
	}
}
